package com.fs.base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.fs.base.param.SysConstant;
import com.fs.base.pojo.Modle;
import com.fs.base.pojo.ModleField;
import com.fs.base.repository.ModleRepository;
import com.fs.base.utils.ModleTools;
import com.fs.base.vo.DataGrid;
import com.fs.base.vo.EasyFormItem;
import com.fs.base.vo.JsonResponse;
import com.fs.base.vo.SysPath;
@Controller
@RequestMapping(SysPath.BACK_MODLE_PATH)
public class ModleController {
	private static final Logger log = Logger.getLogger(ModleController.class);
	
	@Autowired private ModleRepository modleRepository;
	
	@ResponseBody
	@RequestMapping("/fieldList")
	public DataGrid fieldList(String className){
		
		DataGrid grid = new DataGrid();
		try {
			List<ModleField> list = ModleTools.modleField(className);
			grid = DataGrid.gridBuilder(list);
		} catch (Exception e) {
			grid = DataGrid.gridBuilder(new ArrayList<JSONObject>());
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return grid;
	}
	
	@ResponseBody
	@RequestMapping("/formItemList")
	public List<EasyFormItem> formItemList(){
		return EasyFormItem.formItems();
	}
	
	
	@ResponseBody
	@RequestMapping("/init")
	public JsonResponse init(){
		try {
			String[] classNames = ModleTools.modleClassList("com/fs/*/pojo/*.class");
			Long count = 0l;
			List<Modle> list = new ArrayList<Modle>();
			for(String className: classNames){
				count = modleRepository.countByClassName(className);
				if(count==0){
					Modle _modle = Modle.newInstance(className);
					list.add(_modle);
					/*
					 * 动态添加实体模块名称同类名的对应关系
					 */
					SysConstant.MODLE_CLASSNAME.put(_modle.getModleName(), _modle.getClassName());
				} 
			}
			modleRepository.save(list);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return JsonResponse.error(e.getMessage());
		}
		return JsonResponse.success();
	}
	
	@GetMapping("{className}/design")
	public String design(@PathVariable String className,HttpServletRequest request){
		request.setAttribute("className", className);
		return SysPath.BACK_MODLE_PATH.concat("/design");
	}
	
	/*
	 * 平台控件示例
	 */
	@GetMapping("easyUI")
	public String demo(HttpServletRequest request){
		return SysPath.BACK_MODLE_PATH.concat("/easyUI");
	}
	
	@RequestMapping("{className}/gridConfig")
	@ResponseBody
	public String gridPost(@PathVariable String className,HttpServletRequest request){
		String gridJson = request.getParameter("gridJson");
		request.getSession().setAttribute("gridJson", gridJson);
		request.setAttribute("className", className);
		return "s";
	}
	
	@RequestMapping("{className}/grid")
	public String gridSet(@PathVariable String className,HttpServletRequest request){
		request.setAttribute("className", className);
		return SysPath.BACK_MODLE_PATH.concat("/grid");
	}
	
	@GetMapping("{className}/gridParam")
	@ResponseBody
	public JSONObject gridParam(@PathVariable String className,HttpServletRequest request){
		String json = (String)request.getSession().getAttribute("gridParam") ;
		return JSONObject.parseObject(json);
	}
	
}
